package org.spring.springboot.app.community.structure.service.impl;

import org.spring.springboot.app.community.house.dao.CommunityHouseDao;
import org.spring.springboot.app.community.house.domain.po.CommunityHousePO;
import org.spring.springboot.app.community.structure.dao.CommunityStructureDao;
import org.spring.springboot.app.community.structure.domain.po.CommunityStructurePO;
import org.spring.springboot.app.community.structure.domain.vo.*;
import org.spring.springboot.app.community.structure.service.ICommunityStructureService;
import org.spring.springboot.app.sys.office.dao.SysOfficeDao;
import org.spring.springboot.app.sys.office.domain.po.SysOfficePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CommunityStructureServiceImpl implements ICommunityStructureService {

    @Autowired
    private CommunityStructureDao communityStructureDao;

    @Autowired
    private CommunityHouseDao communityHouseDao;
    @Autowired
    private SysOfficeDao sysOfficeDao;

    @Override
    public List<CommunityStructurePO> findByParentId(String parent_id) {
        return communityStructureDao.findByParent(parent_id);
    }

    @Override
    public List<CommunityStructureHouseVO> findHouseByFloorId(String floorId) {
        return communityStructureDao.findHouseByFloorId(floorId);
    }

    @Override
    public CommunityStructurePO findById(String id) {
        CommunityStructurePO record=new CommunityStructurePO();
        record.setId(id);
        return communityStructureDao.selectByPrimaryKey(id);
    }

    @Override
    public String findByParent(String id) {
        String parentIds="";
            CommunityStructurePO po=communityStructureDao.selectByPrimaryKey(id);
        if(po==null){
            SysOfficePO sys=sysOfficeDao.selectByPrimaryKey(id);
            if(sys.getType().equals("3")){
                parentIds=sys.getParentId();
            }
        }else{
            SysOfficePO sys=sysOfficeDao.selectByPrimaryKey(po.getOfficeId());
            if(po.getType().toString().equals("1")){
                if(sys.getType().equals("3")){
                    parentIds+=sys.getParentId()+","+sys.getId()+",";
                }
            }else{
                parentIds+=sys.getParentId()+","+sys.getId()+",";
                parentIds+=po.getParentIds();
            }
        }
        return parentIds;
    }

    @Override
    public int saveBuild(CommunityStructureBuildVO vo) {
        CommunityStructurePO info=communityStructureDao.selectByPrimaryKey(vo.getInfoId());
        CommunityStructurePO po=new CommunityStructurePO();
        po.preInsert();
        po.setOfficeId(info.getOfficeId());
        po.setName(vo.getName());
        po.setType(Byte.valueOf("2"));
        po.setAreaId(info.getAreaId());
        po.setParentId(info.getId());
        po.setParentIds(info.getId()+",");
        po.setName(vo.getName());
        po.setSort(Byte.valueOf(vo.getSort().toString()));
        communityStructureDao.insertSelective(po);
        return 0;
    }

    @Override
    public int saveFloor(CommunityStructureFloorVO vo) {
        CommunityStructurePO build=communityStructureDao.selectByPrimaryKey(vo.getBuildId());
        CommunityStructurePO po=new CommunityStructurePO();
        po.preInsert();
        po.setOfficeId(build.getOfficeId());
        po.setName(vo.getName());
        po.setType(Byte.valueOf("3"));
        po.setAreaId(build.getAreaId());
        po.setParentId(build.getId());
        po.setParentIds(build.getParentIds()+build.getId()+",");
        po.setName(vo.getName());
        po.setSort(Byte.valueOf(build.getSort()));
        return communityStructureDao.insertSelective(po);
    }

    @Override
    public int saveHouse(CommunityStructureHouseVO vo) {
        try{
            //添加组织机构表
            CommunityStructurePO floor=communityStructureDao.selectByPrimaryKey(vo.getFloorId());
            CommunityStructurePO po=new CommunityStructurePO();
            po.preInsert();
            po.setOfficeId(floor.getOfficeId());
            po.setName(vo.getName());
            po.setType(Byte.valueOf("4"));
            po.setAreaId(floor.getAreaId());
            po.setParentId(floor.getId());
            po.setParentIds(floor.getParentIds()+floor.getId()+",");
            po.setName(vo.getName());
            po.setSort(Byte.valueOf(floor.getSort()));
            communityStructureDao.insertSelective(po);
            //添加房间表
            CommunityHousePO hpo=new CommunityHousePO();
            hpo.preInsert();
            hpo.setId(po.getId());
            if(vo.getMonthCost()!=null){
                BigDecimal month=new BigDecimal(vo.getMonthCost());
                hpo.setMonthCost(month);
            }
            if(vo.getOtherCost()!=null){
                BigDecimal other=new BigDecimal(vo.getOtherCost());
                hpo.setOtherCost(other);
            }
            if(vo.getCostExpireDay()!=null){
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                Date date=sdf.parse(vo.getCostExpireDay());
                hpo.setCostExpireDay(date);
            }
            if(vo.getState()==null){
                hpo.setState(1);
            }else{
                hpo.setState(vo.getState());
            }
            communityHouseDao.insertSelective(hpo);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateBuild(CommunityStructurePO po) {
        return communityStructureDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public int updateFloor(CommunityStructurePO po) {
        return communityStructureDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public int updateHouse(CommunityStructureHouseUpVO vo) {
        try{
            CommunityStructurePO po=new CommunityStructurePO();
            po.setId(vo.getId());
            po.setName(vo.getName());
            po.setSort(Byte.valueOf(vo.getSort()));
            communityStructureDao.updateByPrimaryKeySelective(po);
            CommunityHousePO hpo=new CommunityHousePO();
            hpo.setId(vo.getId());
            if(vo.getMonthCost()!=null && !vo.getMonthCost().toString().equals("")){
                BigDecimal month=new BigDecimal(vo.getMonthCost());
                hpo.setMonthCost(month);
            }else{
                BigDecimal month=new BigDecimal("0.00");
                hpo.setMonthCost(month);
            }
            hpo.setState(vo.getState());
            communityHouseDao.updateByPrimaryKeySelective(hpo);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  0;
    }

    @Override
    public int deleteByParentIds(String id) {
        //删除子类
        CommunityStructurePO po= communityStructureDao.selectByPrimaryKey(id);
        if(po.getParentIds()!=null && po.getParentIds().length()>0){
            int ret= communityStructureDao.deleteByParentIds(po.getParentIds()+","+id);
        }
        //删除本身
        CommunityStructurePO po1=new CommunityStructurePO();
        po1.setId(id);
        po1.setDelFlag("1");
        communityStructureDao.updateByPrimaryKeySelective(po1);
        return 1;
    }

    @Override
    public int deleteFloor(String id) {
        return 0;
    }

    @Override
    public int queryByIdAndType(String id,String sort, String type) {
        return communityStructureDao.queryByIdAndType(id,sort,type);
    }

    @Override
    public int fastSaveBuild(CommunityStructureFastVO vo) {
        //查询楼栋
        CommunityStructurePO po=communityStructureDao.selectByPrimaryKey(vo.getBuildId());
        Integer sort=this.findMaxSort(po.getId(),"3");
        if(sort==null){
            sort=1;
        }
        //添加楼层信息
        for(int i=0;i<vo.getFloorNum();i++){
            CommunityStructurePO floor=new CommunityStructurePO();
            floor.preInsert();
            floor.setAreaId(po.getAreaId());
            floor.setOfficeId(po.getOfficeId());
            floor.setName("第"+(sort+i)+"层");
            floor.setParentId(po.getId());
            floor.setParentIds(po.getParentIds()+po.getId()+",");
            floor.setType(Byte.valueOf("3"));
            floor.setSort(Byte.valueOf((sort+i)+""));
            communityStructureDao.insertSelective(floor);
            for(int j=0;j<vo.getHouseNum();j++){
                CommunityStructurePO house=new CommunityStructurePO();
                house.preInsert();
                house.setAreaId(po.getAreaId());
                house.setOfficeId(po.getOfficeId());
                house.setName((sort+i)+"_"+(j+1)+"房间");
                house.setParentId(floor.getId());
                house.setParentIds(po.getParentIds()+po.getId()+","+floor.getId()+",");
                house.setType(Byte.valueOf("4"));
                house.setSort(Byte.valueOf(j+1+""));
                communityStructureDao.insertSelective(house);
                //插入数据到房间表
                CommunityHousePO hpo=new CommunityHousePO();
                hpo.preInsert();
                hpo.setId(house.getId());
                hpo.setState(1);
                communityHouseDao.insertSelective(hpo);
            }
        }
        return 1;
    }

    @Override
    public Integer findMaxSort(String parentId, String type) {
        return communityStructureDao.findMaxSort(parentId,type);
    }

    @Override
    public CommunityStructureHouseVO findHouseById(String houseId) {
        return communityStructureDao.findHouseById(houseId);
    }
}
